//
//  IdentitycardViewController.m
//  ACE
//
//  Created by Mickey Yin on 2021/12/9.
//

#import "IdentitycardViewController.h"
#import <CoreLocation/CoreLocation.h>
//地理编码头文件
#import <AddressBook/AddressBook.h>
@interface IdentitycardViewController ()<CLLocationManagerDelegate>
@property (strong,nonatomic) CLLocationManager * locationManager;
// 用作地理编码、反地理编码的工具类
@property (nonatomic, strong) CLGeocoder *geoC;
@property (nonatomic, strong) UILabel *locationLable;
@end

@implementation IdentitycardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = UIColor.whiteColor;
    
    self.navigationItem.title = @"身份证识别SDK";
    
    self.locationLable = [[UILabel alloc] init];
    self.locationLable.numberOfLines = 0;
    self.locationLable.textColor = UIColor.cyanColor;
    [self.view addSubview:self.locationLable];
    [self.locationLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
    }];
    
    [self startLocation];
}

-(CLGeocoder *)geoC
{
    if (!_geoC) {
        _geoC = [[CLGeocoder alloc] init];
    }
    return _geoC;
}
-(CLLocationManager *)locationManager
{
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc]init];
        // 设置定位距离过滤参数 （当上次定位和本次定位之间的距离 > 此值时，才会调用代理通知开发者）
        _locationManager.distanceFilter = kCLDistanceFilterNone;
        // 设置定位精度 （精确度越高，越耗电，所以需要我们根据实际情况，设定对应的精度）
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//        _locationManager.allowsBackgroundLocationUpdates = YES;
        _locationManager.delegate = self;
    }
    return _locationManager;
}

- (void)startLocation {
//    请求始终授权
    [self.locationManager requestAlwaysAuthorization];
    
//    请求前台授权
//    [_locationManager requestWhenInUseAuthorization];
    
    // 2.判断定位服务是否可用
    if([CLLocationManager locationServicesEnabled])
    {
//        多次定位
        [self.locationManager startUpdatingLocation];
//        只定位一次
//        [self.locationManager requestLocation];
    }else
    {
        NSLog(@"不能定位呀");
        [self locationImpower];
    }
}

#pragma mark 定位授权
- (void)locationImpower {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还未开启定位服务，是否需要开启？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    UIAlertAction *queren = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSURL *setingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        [[UIApplication sharedApplication]canOpenURL:setingsURL];
    }];
    [alert addAction:cancel];
    [alert addAction:queren];
    [self.navigationController presentViewController:alert animated:YES completion:nil];
}

#pragma mark location代理 当定位失败后调用此方法
- (void)locationManager:(nonnull CLLocationManager *)manager didFailWithError:(nonnull NSError *)error
{
    [self locationImpower];
    NSLog(@"定位失败--%@", error.localizedDescription);
}

//定位到的代理方法
-(void)locationManager:(nonnull CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations
{
    CLLocation *location = locations.lastObject;
//    self.latitude.text = [NSString stringWithFormat:@"%f",location.coordinate.latitude];
//    self.longitude.text = [NSString stringWithFormat:@"%f",location.coordinate.longitude];
    NSLog(@"位置信息维度%f,经度%f,海拔%f,方向%f,速度%fM/s,时间%@,水平精确度%f",location.coordinate.latitude,location.coordinate.longitude,location.altitude,location.course,location.speed,location.timestamp,location.horizontalAccuracy);
    self.locationLable.text = [NSString stringWithFormat:@"位置信息维度%f,经度%f,海拔%f,方向%f,速度%fM/s,时间%@,水平精确度%f",location.coordinate.latitude,location.coordinate.longitude,location.altitude,location.course,location.speed,location.timestamp,location.horizontalAccuracy];
}

@end
